package com.yhj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhj.model.Product;
import com.yhj.model.YourEntity;
import com.yhj.service.ProductService;
import com.yhj.service.es.YourEntityRepository;
import com.yhj.vo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Optional;

/**
 * @author yaohj
 * @date 2024年11月20日 13:45
 */
@RestController
@Slf4j
@RequestMapping("product")
@Validated
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private YourEntityRepository yourEntityRepository;

    @RequestMapping("/get/{id}")
    public String getProduct(@PathVariable Integer id){
        log.info("不知道怎么说");
        Product productById = productService.getProductById(id);

        return productById.toString();
    }

    @RequestMapping("/get/plus/{id}")
    public String getProductPlus(@PathVariable Integer id){
        log.info("不知道怎么说");
        Product productById = productService.getProductPlusById(id);

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(qw -> qw.eq("id", 1));

        List<Product> userList = productService.list(queryWrapper);



        return productById.toString();
    }


    @RequestMapping("/add/new/plus")
    public boolean addProductPlus(){
        log.info("不知道怎么说");
        Product product= new Product();
        product.setCommodityName("炒鸡测试");
        boolean save = productService.save(product);

        return save;
    }

    @RequestMapping("/redis/{id}")
    public String getRedisData(@PathVariable String id){
        redisTemplate.opsForValue().set("yhj:admin:id", id);
        return redisTemplate.opsForValue().get("yhj:admin:id");
    }

    @GetMapping("/cuowu")
    public String getErrorData(@NotEmpty String id){
        int a = 1/Integer.parseInt(id);
        return "";
    }


    @GetMapping("/result")
    public CommonResult<String> getResultData(String id){
        return CommonResult.success(id);
    }

    @GetMapping("/es/search")
    public YourEntity esSearch(String id){
        YourEntity yourEntity= new YourEntity();
        yourEntity.setId("1");
        yourEntity.setFiled1("123");
        yourEntityRepository.save(yourEntity);
        Optional<YourEntity> byId = yourEntityRepository.findById("1");
        return byId.get();
    }
}
